﻿using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace CodeContrib.Web.Mvc.Routing
{
	public class NotEqualRouteConstraint : IRouteConstraint
	{
		private readonly object[] _matches;

		public NotEqualRouteConstraint(params object[] matches)
		{
			_matches = matches;
		}

		public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
		{
			if (values[parameterName]
			    == UrlParameter.Optional)
			{
				return true;
			}

			foreach (var match in _matches)
			{
				if (Regex.IsMatch(values[parameterName].ToString(), string.Format("^{0}$", match), RegexOptions.IgnoreCase))
				{
					return false;
				}
			}

			return true;
		}
	}
}